package com.nativetalk.controller.app.three.teacher.comment;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.comment.TdNtCommentPage;
import com.nativetalk.bean.comment.TdNtTeacherComment;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.controller.app.two.setmeal.TwoSetmealController;
import com.nativetalk.service.three.comment.ThreeCommentService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeTeacherCommentController {
    private static Logger logger = Logger.getLogger(ThreeTeacherCommentController.class);
    @Resource
    private ThreeCommentService threeCommentService;

    /**
     * @return RetInfo
     * @Purpose 老师评价
     * @version 3.2
     * @time 2016-08-29
     * @author lizhun
     */
    @RequestMapping(value = "/app/8/teatoken/study/comment", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo comment(@RequestBody TdNtTeacherComment tdNtTeacherComment,
                           @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtTeacherComment));

            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("token is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) o;

            tdNtTeacherComment.setTea_id(tdNtTeacherResult.getTea_id());

            if (tdNtTeacherComment.getReserve_id() == null || tdNtTeacherComment.getReserve_id() == 0) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("-1");
                retInfo.setTip("reserve_id cann't be null");
                return retInfo;
            }

            if (tdNtTeacherComment.getMemb_id() == null || tdNtTeacherComment.getMemb_id() == 0) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("-1");
                retInfo.setTip("memb_id cann't be null");
                return retInfo;
            }

            if (tdNtTeacherComment.getCou_id() == null || tdNtTeacherComment.getCou_id() == 0) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("-1");
                retInfo.setTip("cou_id cann't be null");
                return retInfo;
            }

            if (StringUtils.isBlank(tdNtTeacherComment.getCom_content())) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("-1");
                retInfo.setTip("com_content cann't be null");
                return retInfo;
            }

            if (tdNtTeacherComment.getCom_content().length()>10240) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("-1");
                retInfo.setTip("com_content is too long");
                return retInfo;
            }

            if (tdNtTeacherComment.getCom_grade() == null || tdNtTeacherComment.getCom_grade() == 0) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("-1");
                retInfo.setTip("com_grade cann't be null");
                return retInfo;
            }

            if (tdNtTeacherComment.getIs_trial() == null) {
                RetInfo retInfo = new RetInfo();
                retInfo.setMark("-1");
                retInfo.setTip("is_trial cann't be null");
                return retInfo;
            }

            RetInfo retInfo;
            retInfo = threeCommentService.teahcerComment(tdNtTeacherComment);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
}


